package com.systop.student.controller;

import com.systop.common.pojo.student_elective;
import com.systop.common.util.ResponseResult;
import com.systop.student.service.student_electiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class student_electiveController {
    @Autowired
    private student_electiveService studentElectiveService;
    //根据sno查询
    @PostMapping(value = "/sxlistid")
    @ResponseBody
    public ResponseResult sxlistid(@RequestParam(value = "sno") String sno) {
        if (studentElectiveService.studentSupList(sno)==null){
            return  ResponseResult.build(500,"查询失败");
        }else{
            return  ResponseResult.build(200,"查询成功",studentElectiveService.studentSupList(sno));
        }
    }
    //根据id删除
    @PostMapping(value = "/sxdel")
    @ResponseBody
    public ResponseResult sxdel(@RequestParam(value = "student_elective_id") String student_elective_id) {
       int student_elective_id1 =Integer.parseInt(student_elective_id);
        System.out.println("---"+student_elective_id1);
        int i =studentElectiveService.delStudent(student_elective_id1);
        if (i==0){
            return  ResponseResult.build(500,"查询失败");
        }else{
            return  ResponseResult.build(200,"查询成功",i);
        }
    }
    //添加
    @PostMapping(value = "/sxadd")
    @ResponseBody
    public ResponseResult sxadd(@RequestParam(value = "sno")String sno, @RequestParam(value = "elective_course_id")String elective_course_id
            ,@RequestParam(value = "student_name")String student_name,@RequestParam(value = "remark")String remark) {
        int elective_course_id1 =Integer.parseInt(elective_course_id);
       List<student_elective> list = studentElectiveService.studentSupList(sno);
       for (int i=0;i<list.size();i++){
           if (list.get(i).getElective_course_id() == elective_course_id1){
               return  ResponseResult.build(2000,"重复选课");
           }
       }
        student_elective se = new student_elective();
        se.setSno(sno);
        System.out.println(sno);
        System.out.println(elective_course_id);
        System.out.println(student_name);
        System.out.println(remark);
        se.setElective_course_id(elective_course_id1);
        se.setStudent_name(student_name);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = ft.parse(df.format(new Date()));
            System.out.println(date);
            se.setTime_stamp(date);
            se.setRemark(remark);
            int  i=studentElectiveService.studentAdd(se);
            if (i==0){
                return  ResponseResult.build(500,"添加失败");
            }else{
                return  ResponseResult.build(200,"添加成功",i);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return  ResponseResult.build(500,"时间类型转换失败");
        }
    }
    //根据elective_course_id排课id查询
    @PostMapping(value = "/studentSelective_course_id")
    @ResponseBody
    public ResponseResult studentSelective_course_id(@RequestParam(value = "elective_course_id") String elective_course_id) {
        int elective_course_id1 =Integer.parseInt(elective_course_id);
        System.out.println("---"+elective_course_id1);
        List<student_elective> list =studentElectiveService.studentSelective_course_id(elective_course_id1);
        if (list==null){
            return  ResponseResult.build(500,"查询失败");
        }else{
            return  ResponseResult.build(200,"查询成功",list);
        }
    }

}
